package com.example.fmgidsprototype;

import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.fmgidsprototype.model.Category;
import com.example.fmgidsprototype.model.Venue;
import com.google.android.gms.maps.model.LatLng;

public class VenuesAdapter extends ArrayAdapter<Venue>{
	
	private Context ctx;
	private Category cat;
	private LatLng currentLL;
	
	public VenuesAdapter(Context ctx, List<Venue> venues, LatLng CurrentLatLng){
		super(ctx, R.layout.venue_list_item, venues);
		this.ctx = ctx;
		this.currentLL = CurrentLatLng;
	}
	
	public View getView(int position, View convertView, ViewGroup parent){
		View rowView = convertView;
		Venue venue = this.getItem(position);
		cat = venue.getCategories().get(0);
		
		if(rowView == null){
			LayoutInflater inflater = 
					(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.venue_list_item, parent, false);
			TextView text = (TextView)rowView.findViewById(R.id.venue_text);
			TextView border = (TextView)rowView.findViewById(R.id.venue_item_border);
			ImageView icon = (ImageView)rowView.findViewById(R.id.cat_icon_thumb);
			TextView address = (TextView)rowView.findViewById(R.id.venue_address);
		
			ImageView image = (ImageView)rowView.findViewById(R.id.venue_thumb);
			ViewHolder newHolder = new ViewHolder();
			newHolder.text = text;
			newHolder.image = image;
			newHolder.icon = icon;
			newHolder.border = border;
			newHolder.address = address;
			rowView.setTag(newHolder);
		}
		
		
		float[] results=new float[5];
		//calculate the distance between the venue and My Location
		Location.distanceBetween(currentLL.latitude, currentLL.longitude, venue.getLatitude(), venue.getLongitude(), results);
		String Distance = "";
		
		Float MyDistanceFromCenter = Util.getRadiusMyDistanceFromCenter(currentLL);
			
		
		if (results[0] > 1000F){
			Distance = String.format(Locale.US, "%.2f",results[0]/1000) + " km";	
		}else{
			Distance = String.format(Locale.US, "%.2f",results[0]) + " m";
		}

		String tDistance = Distance;
		if (MyDistanceFromCenter > AppConstants.MaxRadiusFromCenter) {
			Distance = "Afstand van Grote markt: "+ venue.getDistanceFromZero() + " m ";
		}else{
			Distance = "Afstand: "+tDistance;
		}
		
		
		ViewHolder holder = (ViewHolder)rowView.getTag();
		Log.d("GET_VIEW_MSG", "return value: " + venue.getThumbName());
		Log.d("VENUECAT", cat.getName());
		holder.text.setText(venue.getName());
		String imageName = Util.stripExtension(venue.getThumbName());
		holder.image.setImageResource(ctx.getResources().getIdentifier(imageName, "drawable",
				ctx.getPackageName()));
		String iconName = Util.stripExtension(Util.CAT_ICONS.get(cat.getName().toLowerCase()));
		holder.icon.setImageResource(ctx.getResources().getIdentifier(iconName, "drawable",
				ctx.getPackageName()));
		String colorString = Util.COLOR_CODES.get(cat.getName().toLowerCase());
		int c = Color.parseColor(colorString);
		holder.border.setBackgroundColor(c);
		holder.address.setText(venue.getAddress() + "\r\n"+ Distance);
		return rowView;
	}
	
	protected static class ViewHolder{
		protected TextView text;
		protected ImageView image;
		protected ImageView icon;
		protected TextView border;
		protected TextView address;
	}
	
}
